package com.qingfeng.provide.controller;

import com.github.pagehelper.PageInfo;
import com.qingfeng.core.dao.qingfeng.TaskDao;
import com.qingfeng.core.data.ResultData;
import com.qingfeng.core.entity.qingfeng.Task;
import com.qingfeng.provide.constant.MyConstant;
import com.qingfeng.provide.service.TaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

/**
 * @description: 任务
 * @author:LWY
 * @date: 2017/11/15 16:05
 * @modified:
 */
@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    TaskService taskService;
    @Autowired
    TaskDao taskDao;

    /**
     * 发布任务
     *
     * @param request
     * @return
     */
    @RequestMapping("/release")
    public ResultData release(HttpServletRequest request, Task task) throws ParseException {
        //用户id
        String user_id = request.getParameter("id");
      /*  //类型（外卖快递）
        String type = request.getParameter("type");
        //描述
        String task_desc = request.getParameter("task_desc");
        //备注
        String remark = request.getParameter("remark");
        //取货地址
        String from_place = request.getParameter("from_place");
        //取货经度
        double from_longitude = -1;
        if (StringUtils.isNotEmpty(request.getParameter("from_longitude"))) {
            from_longitude = Double.parseDouble(request.getParameter("from_longitude"));
        }
        //取货纬度
        double from_latitude = -1;
        if (StringUtils.isNotEmpty(request.getParameter("from_latitude"))) {
            from_latitude = Double.parseDouble(request.getParameter("from_latitude"));
        }
        //送达地址
        String to_place = request.getParameter("to_place");
        double to_longitude = -1;
        if (StringUtils.isNotEmpty(request.getParameter("to_longitude"))) {
            to_longitude = Double.parseDouble(request.getParameter("to_longitude"));
        }
        //送达纬度
        double to_latitude = -1;
        if (StringUtils.isNotEmpty(request.getParameter("to_latitude"))) {
            to_latitude = Double.parseDouble(request.getParameter("to_latitude"));
        }
        //限制男女（1：女、2：男、3：不限制）
        Integer limit_sex = 3;
        if (StringUtils.isNotEmpty(request.getParameter("limit_sex"))) {
            limit_sex = Integer.parseInt(request.getParameter("limit_sex"));
        }
        //附加标签（挺沉的、大件、代保管）,逗号分割
        String label = request.getParameter("label");
        //商品费用
        BigDecimal cost = null;
        if (StringUtils.isNotEmpty(request.getParameter("cost"))) {
            cost = new BigDecimal(request.getParameter("cost"));
        }
        //打赏费用
        BigDecimal reward = null;
        if (StringUtils.isNotEmpty(request.getParameter("reward"))) {
            reward = new BigDecimal(request.getParameter("reward"));
        }
        //期望完成时间______起
        Date arrive_start = null;
        if (StringUtils.isNotEmpty(request.getParameter("arrive_start"))) {
            arrive_start = DateUtil.parse(request.getParameter("arrive_start"), "yyyy-MM-dd hh:mm:ss");
        }
        //期望完成时间______终
        Date arrive_end = null;
        if (StringUtils.isNotEmpty(request.getParameter("arrive_end"))) {
            arrive_end = DateUtil.parse(request.getParameter("arrive_end"), "yyyy-MM-dd hh:mm:ss");
        }
        //优惠券id
        String coupons_id = request.getParameter("coupons_id");*/
        //支付方式（1：余额支付、2：支付宝、3：微信）
        Integer pay_way = null;
        if (StringUtils.isNotEmpty(request.getParameter("pay_way"))) {
            pay_way = Integer.valueOf(request.getParameter("pay_way"));
        }
        task.setUser_id(user_id);
        taskService.release(task, pay_way);
        return new ResultData(ResultData.SUCCESS, "任务已经发布出去了，等待青蜂侠接单", "");
    }

    /**
     * 任务详情信息
     *
     * @param id 任务id
     * @return
     */
    @RequestMapping("/{id}")
    public ResultData get(@PathVariable String id) {
        taskService.get(id);
        return null;
    }

    /**
     * 我的跑单
     *
     * @param type 类型（1：我发布的任务、2:我的抢单）
     * @return
     */
    @RequestMapping("/my/{type}")
    public ResultData my(HttpServletRequest request, @PathVariable Integer type) {
        String user_id = request.getParameter("id");
        //任务状态（1:未发布状态，2：已经发布了）
        String state = request.getParameter("state");
        String pageNum = request.getParameter("pageNum");
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = "1";
        }
        PageInfo<HashMap<String, Object>> pageInfo = taskDao.my(Integer.valueOf(pageNum), MyConstant.getPageSize(), user_id, type, Integer.valueOf(state));
        return new ResultData(ResultData.SUCCESS, "获取成功", pageInfo);
    }

    /**
     * 退款
     *
     * @param id 任务id
     * @return
     */
    @RequestMapping("/my/refund/{id}")
    public ResultData refund(HttpServletRequest request, @PathVariable String id) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        String user_id = request.getParameter("id");
        String order_id = request.getParameter("order_id");
        //退款时状态（1：未收到货、2：已经收到货）
        int goods_state = Integer.valueOf(request.getParameter("goods_state"));
        String rease = request.getParameter("rease");//逗号分割
        String instruction = request.getParameter("instruction");//退款说明
        taskService.refund(user_id, id, order_id, goods_state, rease, instruction, files);
        return new ResultData(ResultData.SUCCESS, "退款申请提交成功，请等待审核", "");
    }
}
